var mCode = $('#m_Code'),
    _grid = $('#jgTable');

var _Data = [];

$(function(){
    requestAjax();
    _gridInit();
    $('li[data-opt="add"]').on('click', add);
    $('li[data-opt="u_save"],button[data-opt="u_save"]').on('click', save);
    $('.modal-data').draggable();
});

function requestAjax() {
    var _postData = Object.create(postData);
    _postData.Method = 'Q';
    _postData.Content = {
        SearchText: '' 
    };
    _postData.page = 1;
    _postData.rows = 100;
    _postData.sidx = 'Code';
    _postData.sord = 'Asc'

    var url = mdmPath + '/MDM0051/MDM005102';
    $.ajax({
        async: false,
        type: "post",
        url: url,
        data: _postData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                loadAjax(re);
            } else if (re.Status == -999) {
                alert_QX($('body'), re.Message);
            } else {
                alert(re.Message);
            }
        }
    });
}
function loadAjax(re){
    if(re.Content){
        _Data = re.Content.rows;
    }
}
function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.datatype = "local";
    _gridData.rowNum = 100;
    _gridData.colModel = [
        {label:'热搜词', name:'Code', sortable:false},
        {label:'创建时间', name:'CreateDate', sortable:false},
        //{label:'创建用户', name:'CreateUser', sortable:false},
        {label:'更新时间', name:'UpdateDate', sortable:false},
        //{label:'更新用户', name:'UpdateUser', sortable:false},
        {label:'操作', name:'Options', align:'center', width:80, formatter: alarmFormatter, sortable:false}
    ];
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            data : _Data
        });
    };
    _gridData.loadComplete = function(xhr){ 
        var rowData = _gridData.grid.jqGrid("getRowData");
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click',function(){
            var rowId = $(this).parent().parent()[0].id;
            _Data.forEach(function(v,i,a){
                console.log(v)
                v.Code===rowData[rowId-1].Code&&_Data.splice(i,1);
            });
            _gridData.grid.jqGrid("delRowData",rowId);
            console.log(_Data)
        });
        _gridData.grid.jqGrid('sortableRows',{
            update:function(ev,ui){
                var item = ui.item[0],
                ri = item.rowIndex,
                itemId = item.id;
            }
        });
    };
    gridInit(_gridData);
}

//Options
function add() {
    $('.modal-overlay').removeClass('hidden');
    $('#addBtn').unbind('click').bind('click', function(){
        if(mCode.val()){
            _Data.push({Code:mCode.val()});
            _Data = _Data.unique('Code');
            _grid.jqGrid("clearGridData").trigger("reloadGrid");
            $('.modal-overlay').addClass('hidden');
        }else {
            myAlert('请输入热搜词')
        }
    })
}
function save() {
    var rowData = _grid.jqGrid('getRowData');
    var requestData = Object.create(postData);
    requestData.Method = 'U';
    var arr = [];
    rowData.forEach(function(v,i,a){
        arr.push({Code:v.Code});
    });
    requestData.Content = arr;
    console.log(requestData);
    $.ajax({
        type: "post",
        url: mdmPath+'/MDM0051/MDM005101',
        data: requestData,
        dataType: "json",
        success: function (re) {
            if (re.Status == 200) {
                myAlert('保存成功');
            } else {
                myAlert(re.Message);
            }
        }
    });
}

//样式
function gridSize() {
    _grid.setGridWidth($('.u_toolbar').width());
}